function [theta, residuals]=qreg_NAG(X,y,tau,theta0,d_resid)

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % INITIALIZE
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
m=size(X,2);
assert(size(tau,1)==1)

n_input=int64(size(X,1));
m_input=int64(m);
ntau=int64(1);

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % SET g02qg OPTIONS
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%

sorder = int64(1);
c1 = 'n';
weight = 'u';
isx = int64(ones(m,1))  ;
state = zeros(1, 1, 'int64');
ip = m;
ip_input=int64(ip);
iopts = zeros(100, 1, 'int64');
opts = zeros(100, 1);

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Initialize the optional argument array
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%

[iopts, opts, ~] = nag_correg_optset('Initialize = nag_correg_quantile_linreg', iopts, opts);

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Set options
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%

[iopts, opts, ~] = nag_correg_optset('Matrix Returned = None', iopts, opts);
[iopts, opts, ~] = nag_correg_optset('Interval Method = none', iopts, opts);
[iopts, opts, ~] = nag_correg_optset('Iteration Limit = 500', iopts, opts);

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Starting point needed?
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%

if max(isnan(theta0))==1,
    [iopts, opts, ~] = nag_correg_optset('Calculate Initial Values = Yes', iopts, opts);
    theta0=NaN*zeros(ip,1);
else
    [iopts, opts, ~] = nag_correg_optset('Calculate Initial Values = No', iopts, opts);
end;

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Residuals needed?
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%

if d_resid==1,
    [iopts, opts, ~] = nag_correg_optset('Return Residuals = Yes', iopts, opts);
else
    [iopts, opts, ~] = nag_correg_optset('Return Residuals = No', iopts, opts);
end;

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % CALL ROUTINE
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%

[~, b, ~, ~, ~, res, ~, ~, ifail] = ...
    nag_correg_quantile_linreg(sorder, c1, weight, X, isx, y, tau, theta0, iopts, opts, state, ...
                               'n', n_input, 'm', m_input, 'ip', ip_input, 'ntau', ntau);

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % OUTPUT
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%

if ifail==0,
    theta=b;
    residuals=res;
else
    theta=NaN*b;
    residuals=NaN*res;
end;

    %
